package gateway.internal.config.impl;

import gateway.config.IdConfig;

import java.util.Map;
import java.util.Objects;

/**
 * @author Jin Zheng
 * @since 1.0 2020-09-15
 */
public abstract class DefaultIdConfig implements IdConfig
{
	protected String id;
	protected String name;
	protected Map<String, String> args = Map.of();

	public DefaultIdConfig()
	{
	}

	@Override
	public String id()
	{
		return id;
	}

	@Override
	public String name()
	{
		return name;
	}

	@Override
	public Map<String, String> args()
	{
		return args;
	}

	@Override
	public String getStringArg(String key)
	{
		if (args == null)
			return null;

		return args.get(key);
	}

	@Override
	public int getIntArg(String key, int defaultVal)
	{
		String val = this.getStringArg(key);
		if (val == null || val.isEmpty())
			return defaultVal;

		try
		{
			return Integer.parseInt(val);
		}
		catch (NumberFormatException e)
		{
			e.printStackTrace();
			return defaultVal;
		}
	}

	@Override
	public boolean equals(Object o)
	{
		if (this == o)
		{
			return true;
		}
		if (o == null || getClass() != o.getClass())
		{
			return false;
		}
		DefaultIdConfig that = (DefaultIdConfig) o;
		return id.equals(that.id);
	}

	@Override
	public int hashCode()
	{
		return Objects.hash(id);
	}

	@Override
	public String toString()
	{
		return String.format("%s{id: %s, name: %s}", this.getClass().getSimpleName(), id, name);
	}

	public void setId(String id)
	{
		this.id = id;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public void setArgs(Map<String, String> args)
	{
		this.args = args;
	}

	@Override
	public boolean isInline()
	{
		return id == null || id.isEmpty();
	}
}
